﻿namespace Report_Sample
{
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Data;
    using System.Linq;
    using System.Resources;

    using CrumbleLibrary;

    /// <summary>
    /// The prototype report.
    /// </summary>
    [Export("Reports", typeof(IReport))]
    [ExportMetadata("Id", "AnUniqueID_CanbeAnyString")]
    [ExportMetadata("Reportname", "Example_Report")]
    [ExportMetadata("ReportType", HelperLibrary.ReportType.MSRS)]
    public class Report : TemplateReport
    {
        /// <summary>
        /// Says if the Report should be loaded after startup.
        /// </summary>
        public const bool LoadReportOnStartup = true;

        /// <summary>
        /// The list parameter.
        /// </summary>
        private Dictionary<string, FilterParameterItem> internalParameterList =
            new Dictionary<string, FilterParameterItem>();

        public override Dictionary<string, FilterParameterItem> ParameterItems
        {
            get
            {

                this.AttachDynamicFilter(ref this.internalParameterList);
                return this.internalParameterList;
            }

            set
            {
                this.internalParameterList = value;
            }
        }
        
        public override Dictionary<string, List<FilterParameterItem>> SetFilter()
        {
            var filterSetting = new Dictionary<string, List<FilterParameterItem>>
                                    {
                                        {
                                            "MiscFilterID", // Misc
                                            new List<FilterParameterItem>
                                            {
                                                new FilterParameterItem(HelperLibrary.FilterParameterType.FilterDynamic, "fDynamicCheckbox", true), 
                                                new FilterParameterItem(HelperLibrary.FilterParameterType.FilterDynamic, "strDynamicTextbox", "Irgend ein text"),
                                                new FilterParameterItem(HelperLibrary.FilterParameterType.FilterDynamic, "eDynComboBox", "2"),
                                            }
                                        }
                                    };

            return filterSetting;
        }

        public override ResourceManager GetInternalTranslaion()
        {
            return ReportTranslation.ResourceManager;
        }

        public override object GetDataInstruction()
        {
            var dataTable = new DataTable();
            return new Microsoft.Reporting.WinForms.ReportDataSource { Name = "ReportTable", Value = dataTable };
        }

        public override Dictionary<string, FilterParameterItem> ConvertValues(Dictionary<string, FilterParameterItem> originParameterItems)
        {

            var tempList = new Dictionary<string, FilterParameterItem>(originParameterItems);
            foreach (var item in originParameterItems.Where(item => item.Value.Type == HelperLibrary.FilterParameterType.FilterDynamic))
            {
                switch (item.Key)
                {
                    case "fDynamicCheckbox":
                        {
                            if (tempList.ContainsKey("repDynamicCheckbox"))
                            {
                                tempList.Remove("repDynamicCheckbox");
                            }

                            var myValue = item.Value.Value as FilterItemDynamic;
                            if (myValue != null)
                            {
                                tempList.Add("repDynamicCheckbox", new FilterParameterItem(HelperLibrary.FilterParameterType.Report, "strCheckBoxExpression", myValue.Value));
                            }
                        }

                        break;
                    case "strDynamicTextbox":
                        {
                            if (tempList.ContainsKey("repDynamicTextbox"))
                            {
                                tempList.Remove("repDynamicTextbox");
                            }

                            var myValue = item.Value.Value as FilterItemDynamic;
                            if (myValue != null)
                            {
                                tempList.Add("repDynamicTextbox", new FilterParameterItem(HelperLibrary.FilterParameterType.Report, "strDynamicTextbox", myValue.Value));
                            }
                        }

                        break;
                    case "eDynComboBox":
                        {
                            if (tempList.ContainsKey("repDynComboBox"))
                            {
                                tempList.Remove("repDynComboBox");
                            }
                            
                            var myValue = item.Value.Value as FilterItemDynamic;
                            if (myValue != null)
                            {
                                tempList.Add("repDynComboBox", new FilterParameterItem(HelperLibrary.FilterParameterType.Report, "eDynComboBox", myValue.Value));
                            }
                        }

                        break;
                }
            }
            return tempList;
        }

        protected void AttachDynamicFilter(ref Dictionary<string, FilterParameterItem> parameterList)
        {
            var dictionary1 = new Dictionary<string, string>
                                  {
                                      {
                                          "0",
                                          "Something"
                                      },
                                      {
                                          "1",
                                          "Nothing"
                                      },
                                      {
                                          "2",
                                          "Whatever"
                                      },
                                      {
                                          "3",
                                          "I'm not here"
                                      }
                                  };

            if (this.internalParameterList.ContainsKey("eDynComboBox"))
            {
                this.internalParameterList.Remove("eDynComboBox");
            }

            this.internalParameterList.Add(
                "eDynComboBox",
                new FilterParameterItem(
                    HelperLibrary.FilterParameterType.FilterDynamic,
                    "eDynComboBox",
                    new FilterItemDynamic(
                        "eDynComboBox",
                        ReportTranslation.filter_ComboBox,
                        HelperLibrary.FilterType.ComboBox,
                        "0",
                        dictionary1)));

            if (this.internalParameterList.ContainsKey("fDynamicCheckbox"))
            {
                this.internalParameterList.Remove("fDynamicCheckbox");
            }

            this.internalParameterList.Add(
                "fDynamicCheckbox",
                new FilterParameterItem(
                    HelperLibrary.FilterParameterType.FilterDynamic,
                    "fDynamicCheckbox",
                    new FilterItemDynamic(
                        "fDynamicCheckbox", ReportTranslation.filter_CheckBox, HelperLibrary.FilterType.CheckBox, true)));

            if (this.internalParameterList.ContainsKey("strDynamicTextbox"))
            {
                this.internalParameterList.Remove("strDynamicTextbox");
            }

            this.internalParameterList.Add(
                "strDynamicTextbox",
                new FilterParameterItem(
                    HelperLibrary.FilterParameterType.FilterDynamic,
                    "strDynamicTextbox",
                    new FilterItemDynamic(
                        "strDynamicTextbox", ReportTranslation.filter_Textbox, HelperLibrary.FilterType.Textbox, "Just some text")));
        }

    }
}
